package customer.service;

import java.util.ArrayList;
import java.util.Iterator;

import customer.domain.Customer;
import customer.exception.CustomerNotFoundException;
import customer.exception.RequiredInputException;

public class CustomerManagerArrayList {

	private ArrayList<Customer> list;
	
	public CustomerManagerArrayList(){

		list = new ArrayList<Customer>(100);
	}
	public CustomerManagerArrayList(int length){

		list = new ArrayList<Customer>(length);
	}

	public void registerCustomer(Customer customer) throws RequiredInputException, CustomerNotFoundException{
	
		String rn = customer.getResidentNumber();
		if(rn == null){
			throw new RequiredInputException("�ֹ� ��ȣ�� �ʼ� �Է� �����Դϴ�. Ȯ���� �ٽ� �����ϼ���");
		}
		Customer cc = 
				searchCustomerByResidentNumber(customer.getResidentNumber());
		if(cc!=null){
			throw new CustomerNotFoundException("���� �ֹι�ȣ�� �������� ��ϵǾ� �ֽ��ϴ�.");
		}
		list.add(customer);
	}
	
	public void printCustomerList(){
		for(int idx = 0; idx < list.size(); idx++){
			System.out.println(list.get(idx));
		}
	}
	
	public Customer searchCustomerByResidentNumber(String residentNumber){
		
		for(int idx = 0; idx < list.size(); idx++){
			
			if(residentNumber.equals( list.get(idx).getResidentNumber())){
				return list.get(idx);
			}
		}
		return null;
	}
		
	public ArrayList<Customer> searchCustomerByName(String name){
		ArrayList<Customer> returnList = new ArrayList<Customer>();
		for(Customer c : list){
			if(name.equals(c.getName())){
				returnList.add(c);
			
			}
		}
		return returnList;
	}
	
	public void updateCustomer(Customer customer) throws CustomerNotFoundException, RequiredInputException{
		
		if(customer.getResidentNumber()==null||customer.getResidentNumber().length()==0){
			throw new RequiredInputException("������ ����� �ֹι�ȣ�� �ʼ� �Է� �����Դϴ�.");
		}
		if(searchCustomerByResidentNumber(customer.getResidentNumber())==null){
			throw new CustomerNotFoundException("������ ���� ã�� �� �����ϴ�.");
		}

		for(int idx = 0; idx < list.size(); idx++){
			if(customer.getResidentNumber().equals(list.get(idx).getResidentNumber())){
				list.set(idx, customer);
				break;//return
			}
		}
		
	}

	public void removeCustomerByResidentNumber(String residentNumber) throws CustomerNotFoundException{
		
		Customer c = searchCustomerByResidentNumber(residentNumber);
		if(c==null){
			throw new CustomerNotFoundException("������ ���� ã�� �� �����ϴ�.");
		}
		list.remove(c);
	}
}